<?php
namespace Admin\Controller;

use Think\Think;

class CommentController extends AdminController {

    public function index() {
        $this->article();
    }

    public function article() {
        $keyword = I('keyword');
        $user_id = I('user_id', 0, 'intval');
        $prefix = C('DB_PREFIX');
        $where = ["A.data_type" => 'article'];
        if ($keyword) {
            $where["A.content"] = ['like', "%{$keyword}%"];
        }
        if ($user_id) {
            $where["A.user_id"] = $user_id;
        }
        $total = M('Comment')->alias('A')->where($where)->count();
        $Page = new \Think\Page($total, $this->page_size);
        $result = M('Comment')->alias('A')->field("A.*,B.title,C.nickname")->where($where)
            ->join($prefix . 'article as B ON A.data_id = B.id', 'left')
            ->join($prefix . 'user as C ON A.user_id = C.id', 'left')
            ->page($Page->nowPage, $Page->listRows)->order("id DESC")->select();
        $category = S('ArticleCategory');
        foreach ($result as $key => $val) {
            $catid = M('Article')->where(['id' => $val['data_id']])->getField('category_id');
            $category_url = $category[$catid]['url'];
            $category_url || $category_url = 'article';
            $result[$key]['url'] = U($category_url . '/view/' . $val['data_id']);
        }
        $this->assign('result', $result);
        $this->assign('page', $Page->show());
        $this->display();
    }

    public function active() {
        $cpage = (int)$_GET['p'] > 0 ? (int)$_GET['p'] : 1;

        $where = [];

        $where['A.data_type'] = 'active';
        $result = M('Comment')->alias('A')->field('A.*,B.nickname,C.activity_name')
            ->join(C('DB_PREFIX') . 'user as B ON A.user_id = B.id', 'left')
            ->join(C('DB_PREFIX') . 'supplier_activity as C ON A.data_id = C.id', 'left')
            ->where($where)->order('id DESC')->page($cpage, $this->page_size)->select();

        $count = M('Comment')->alias('A')->where($where)->count();
        $Page = new \Think\Page($count, $this->page_size);

        $this->assign([
            'result' => $result,
            'page' => $Page->show(),
        ]);
        $this->display();
    }


    public function delete() {
        $id = I('id', 0, 'intval');
        if (!is_array($id)) {
            $id = [$id];
        }
        M('Comment')->where(['id' => ['in', $id]])->delete();
        $this->success('删除成功');
    }

}

